package controller

import (
	"fmt"
	"log"
	"testhttp/dao"
	"time"

	"github.com/kataras/iris/v12"
)

type MembersListParam struct {
	Page              int               `json:"page"`         //页码
	Limit             int               `json:"limit"`        //单页显示行数
	MemberSearchParam MemberSearchParam `json:"searchParams"` //查询条参数
}
type MemberSearchParam struct {
	Username  string `json:"username"` //用户姓名
	UserPhone string `json:"phone"`    //用户手机号
}

// 列表功能，参数pagesize，page
// func ListMember(ctx iris.Context) {
// 	page, _ := ctx.URLParamInt("page")
// 	limit, _ := ctx.URLParamInt("limit")

// 	members, count := dao.ListMember("", "", limit, page)
// 	// count := dao.CountMems()

// 	ctx.JSON(iris.Map{
// 		"code":  0,
// 		"msg":   "",
// 		"count": count,
// 		"data":  members,
// 	})
// }

func ListMember(ctx iris.Context) {
	var memberlistparam MembersListParam
	err := ctx.ReadJSON(&memberlistparam)
	queryparam := " is_deleted = 0 "
	log.Print("Debug param:", memberlistparam)
	if err != nil && !iris.IsErrPath(err) {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.WriteString(err.Error())
	} else {
		log.Print("Debug ListParam:", memberlistparam)
		if memberlistparam.MemberSearchParam.Username != "" {
			queryparam = queryparam + "and mem_name like '%" + memberlistparam.MemberSearchParam.Username + "%'"
			log.Print("Debug queryparam1:", queryparam)
		}
		if memberlistparam.MemberSearchParam.UserPhone != "" {
			queryparam = queryparam + "and mem_phone like '%" + memberlistparam.MemberSearchParam.UserPhone + "%'"
			log.Print("Debug queryparam2:", queryparam)
		}

		res, count := dao.ListMember(fmt.Sprint(queryparam), memberlistparam.Limit, memberlistparam.Page)
		ctx.JSON(iris.Map{
			"code":  0,
			"msg":   "OK",
			"count": count,
			"data":  res,
		})
	}
}

func Search(ctx iris.Context) {
	// username := ctx.URLParam("username")
	// phone := ctx.URLParam("phone")
	// page, _ := ctx.URLParamInt("page")
	// limit, _ := ctx.URLParamInt("limit")

	// if username == "" && phone == "" {
	// members, count := dao.ListMember(username, phone, limit, page)
	// count := dao.CountMems()

	// ctx.JSON(iris.Map{
	// 	"code":  0,
	// 	"msg":   "OK",
	// 	"count": count,
	// 	"data":  members,
	// })

	// } else {
	// 	search := dao.FindMember(username, phone)

	// 	ctx.JSON(iris.Map{
	// 		"code":  0,
	// 		"msg":   "OK",
	// 		"count": len(search),
	// 		"data":  search,
	// 	})
	// }

}

func AddMember(ctx iris.Context) {
	var members []dao.Member
	err := ctx.ReadJSON(&members)
	now := time.Now()
	if err != nil && !iris.IsErrPath(err) {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.WriteString(err.Error())
	} else {
		man := len(members) //提交数据个数，数组长度
		z := 0              //有效计数
		var querydata []dao.Member
		for x := 0; x < len(members); x++ {
			// 验证用户名、手机号是否为空
			members[x].CreateTime = now
			members[x].UpdateTime = now
			members[x].IsDeleted = 0
			if members[x].MemName == "" || members[x].MemPhone == "" {
				// querydata = append(querydata, members[x])
			} else if dao.GetMems(members[x].MemPhone) {

				log.Printf("Debug getmem: %s %t", members[x].MemPhone, dao.GetMems(members[x].MemPhone))
				// querydata = append(querydata, members[x])
				z++
			} else {
				querydata = append(querydata, members[x])
				z++
			}
		}
		y := len(querydata)
		// log.Print("Debug querydata:", querydata)
		dao.InsertMembers(querydata)

		ctx.JSON(iris.Map{
			"code":   0,
			"msg":    "OK",
			"submit": man, //提交数据条数
			"valid":  z,   //有效数据条数
			"new":    y,   //新数据条数
		})
	}
}
func DelMember(ctx iris.Context) {
	var members []dao.Member
	err := ctx.ReadJSON(&members)
	now := time.Now()
	if err != nil && !iris.IsErrPath(err) {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.WriteString(err.Error())
	} else {
		for x := 0; x < len(members); x++ {
			members[x].UpdateTime = now
			members[x].IsDeleted = 1
		}
	}
	fmt.Print("Debug Del mem :", members, "\n")
	dao.UpdateMembers(members)
}

func ViewMember(ctx iris.Context) {
	uid := ctx.URLParam("ruleuid")
	ctx.JSON(dao.ViewMember(uid))
}

func UpdateMember(ctx iris.Context) {
	var members []dao.Member
	err := ctx.ReadJSON(&members)
	now := time.Now()
	if err != nil && !iris.IsErrPath(err) {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.WriteString(err.Error())
	} else {
		for x := 0; x < len(members); x++ {
			members[x].UpdateTime = now
		}
	}
	fmt.Print("Debug update mem :", members, "\n")
	dao.UpdateMembers(members)
}
